<?php
declare (strict_types=1);

namespace app\admin\model\iot;

use aliyun\AliyunOss;
use app\BaseModel;

/**
 * 取袋路径表模型
 */
class ScanBagWay extends BaseModel
{

    /**
     * 取袋路径定义
     * @var array|array[]
     */
    public static array $ScanBagWayArr = [
        ['key' => 'V1_Way_1_0', 'name' => '默认取袋路径', 'remark' => '默认取代路径',],
        ['key' => 'V1_Way_1_1', 'name' => 'E领袋取袋路径', 'remark' => 'E领袋取袋路径',],
        ['key' => 'V1_Way_2_0', 'name' => '地理位置路径', 'remark' => '地理位置路径',],
        ['key' => 'V1_Way_3_0', 'name' => '防止机刷路径', 'remark' => '防止机刷路径',],
        ['key' => 'V1_Way_4_0', 'name' => '默认广告小程序路径(自营)', 'remark' => '默认广告小程序路径(自营)',],
        ['key' => 'V1_Way_4_1', 'name' => 'H5跳广告小程序或默认出袋', 'remark' => 'H5跳广告小程序或默认出袋',],
        ['key' => 'V1_Way_4_2', 'name' => '广告小程序医院路径', 'remark' => '广告小程序医院路径',],
        ['key' => 'V1_Way_4_3', 'name' => '广告小程序医院路径(仅首次)', 'remark' => '广告小程序医院路径(仅首次)',],
        ['key' => 'V1_Way_4_4', 'name' => '广告小程序路径(海南0)', 'remark' => '广告小程序路径(海南0)',],
        ['key' => 'V1_Way_4_4_1', 'name' => '广告小程序路径(海南1)', 'remark' => '广告小程序路径(海南1)',],
        ['key' => 'V1_Way_4_4_2', 'name' => '广告小程序路径(海南1_验证码)', 'remark' => '广告小程序路径(海南_验证码)',],
        ['key' => 'V1_Way_4_5', 'name' => 'e家集市商城', 'remark' => 'e家集市商城',],
        ['key' => 'V2_Way_1_0', 'name' => '基础取袋路径', 'remark' => '基础取袋路径',],
        ['key' => 'V2_Way_2_0', 'name' => '全国通用取袋路径', 'remark' => '全国通用取袋路径',],
        ['key' => 'V1_Way_5_1', 'name' => '绿葆公众运营路径', 'remark' => '绿葆公众号运营路径',],
        ['key' => 'V1_Way_5_2', 'name' => '默认路径', 'remark' => '默认路径',],
        ['key' => 'V1_Way_5_3', 'name' => '默认路径（有定位）', 'remark' => '默认路径（有定位）',],
        ['key' => 'V1_Way_5_4', 'name' => '5_4', 'remark' => '5_4',],
        ['key' => 'V1_Way_5_5', 'name' => '5_5', 'remark' => '5_5',],
        ['key' => 'V1_Way_5_6', 'name' => '5_6', 'remark' => '5_6',],
        ['key' => 'V1_Way_5_7', 'name' => '5_7', 'remark' => '5_7',],
        ['key' => 'V1_Way_7_0', 'name' => '7_0', 'remark' => '7_0',],
        ['key' => 'V1_Way_7_1', 'name' => '7_1', 'remark' => '7_0',],
        ['key' => 'V1_Way_7_2', 'name' => '7_2', 'remark' => '7_2',],
        ['key' => 'V1_Way_7_3', 'name' => '7_3', 'remark' => '7_3',],
        ['key' => 'V1_Way_7_4', 'name' => '7_4', 'remark' => '7_4',],
        ['key' => 'V1_Way_7_5', 'name' => '7_5', 'remark' => '7_5',],
        ['key' => 'V1_Way_7_6', 'name' => '7_6', 'remark' => '7_6',],
        ['key' => 'V1_Way_7_7', 'name' => '7_7', 'remark' => '7_7',],
        ['key' => 'V1_Way_7_8', 'name' => '7_8', 'remark' => '7_8',],
        ['key' => 'V1_Way_6_0', 'name' => '复杂流程(积分任务)', 'remark' => '复杂流程(积分任务)',],
        ['key' => 'V2_Way_3_0', 'name' => 'V2_Way_3_0', 'remark' => 'V2_Way_3_0',],
        ['key' => 'V2_Way_4_0', 'name' => 'V2_Way_4_0', 'remark' => 'V2_Way_4_0',],
        ['key' => 'V2_Way_5_0', 'name' => 'V2_Way_5_0', 'remark' => '2.0纯支付',],
        ['key' => 'V1_Way_8_0', 'name' => 'V1_Way_8_0', 'remark' => '1.0纯支付路径',],
    ];

    /**
     * 获取列表
     * @param array $where
     * @return array
     */
    public static function getList(array $where): array
    {
        $data = self::where($where)
            ->order('key asc')
            ->select()
            ->toArray();
        $AliyunOss = new AliyunOss();
        //遍历数组
        foreach ($data as &$v) {
            //解析value信息
            $v['mini_advert'] = json_decode($v['mini_advert'], true) ?: [];
            //补充二维码图片信息
            $v['mini_qrcode_url'] = $AliyunOss->getObjectUrl($v['mini_qrcode']);
            //视频号或其他的二维码链接
            $v['video_url_qrcode'] = $v['video_url'] ? $AliyunOss->getObjectUrl($v['video_url']) : '';
            //二次关注兜底二维码
            $v['secondary_url_qrcode'] = $v['secondary_url'] ? $AliyunOss->getObjectUrl($v['secondary_url']) : '';
        }
        return $data;
    }

    /**
     * 根据appid获取对应小程序配置
     * @param string $appid
     * @return array
     */
    public static function getAppidWay(string $appid): array
    {
        $where = ['mini_appid' => $appid];
        $WayInfo = ScanBagWay::where($where)->cache(true, 60)->find();
        return $WayInfo ? $WayInfo->toArray() : [];
    }

    /**
     * 通过设备MAC_ID获取取袋路径
     * @param string $mac_id
     * @return array
     */
    public static function getMacIdWay(string $mac_id): array
    {
        //查询取袋路径
        $data = self::alias('a')
            ->field('a.*')
            ->where(['b.mac_id' => $mac_id])
            ->join((new ScanBagWayDevice())->getTable() . ' b', 'a.key = b.key')
            ->cache(true, 60)
            ->find();
        if ($data) {
            return $data->toArray();
        }
        //判断是v1设备还是v2系统设备
        $where = ['mac_id' => $mac_id];
        $device = Device::where($where)->cache(true, 60)->value('id');
        //默认取袋路径
        $key = $device ? 'V2_Way_1_0' : 'V1_Way_4_0';
        $where = ['key' => $key];
        $data = self::where($where)->cache(true, 60)->find();
        return $data ? $data->toArray() : [];
    }

    /**
     * 通过路径标识获取取袋路径
     * @param string $key
     * @return array
     */
    public static function getPathKeyWay(string $key): array
    {
        //查询取袋路径
        $data = self::field('*')
            ->where(['key' => $key])
            ->cache(true, 60)
            ->find();
        return $data ? $data->toArray() : [];
    }

}




